<html>
<script>
var TT_URL="";
var TT_LAST_NOTICE_ID=0;
function loadLastNotice() {
	//Read URL
	TT_URL=localStorage["ttUrl"];
	if (!TT_URL) {
		TT_URL="https://tamtamy.reply.eu";
	}
	var req = new XMLHttpRequest();
	var timestamp = new Date().getTime();
	req.open(
		"GET",
		TT_URL+"/tamtamy/api/notice.xml?limit=1&t="+timestamp,
		true);
	req.onload = loadLastNoticeCallback;
	req.send(null);

	function loadLastNoticeCallback() {
		if (req.status=="401") {
			chrome.browserAction.setBadgeBackgroundColor({color:[255, 0, 0, 255]});
			chrome.browserAction.setBadgeText({text:"!"});
		} else if (req.status=="200") {
			var notices = req.responseXML.getElementsByTagName("it.reply.tt.rest.entity.KcLNotice");
			if (notices.length>0) {
				TT_LAST_NOTICE_ID = notices[0].getElementsByTagName("id")[0].childNodes[0].nodeValue;
				//console.log("TT_LAST_NOTICE_ID="+TT_LAST_NOTICE_ID+" | TT_LAST_READ_NOTICE_ID="+localStorage["TT_LAST_READ_NOTICE_ID"]);
				if (!localStorage["TT_LAST_READ_NOTICE_ID"] || TT_LAST_NOTICE_ID > localStorage["TT_LAST_READ_NOTICE_ID"]) {
					//console.log("new notice to read");
					chrome.browserAction.setBadgeBackgroundColor({color:[0, 255, 0, 255]});
					chrome.browserAction.setBadgeText({text:"new"});
				}
			}
		}
	}
}

loadLastNotice();
setInterval("loadLastNotice()", 300000);
</script>
</html>